package org.itboys.mobile.controller.weixin.order;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.dto.member.SundayWeixinMemberDto;
import org.itboys.mobile.dto.order.CooOrderDto;
import org.itboys.mobile.service.mysql.weixin.order.CooWeixinOrderService;
import org.itboys.mobile.util.HttpRequestUtil;
import org.itboys.mobile.util.MemberUtil;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/12/7
 * 描述：新坐标_微信_订单
 */
@Controller
@RequestMapping("/coo/weixin/order")
public class CooWeixinOrderController extends BaseController{
    @Autowired
    private CooWeixinOrderService orderService;
    @RequestMapping("/index")
    public String index(
            HttpServletRequest request,
            @RequestParam(value="orderStatus",required=false)int orderStatus,
            Model model){
        //获取session中的用户信息
        SundayWeixinMemberDto member = MemberUtil.getUser(request);
        if(LongUtil.isZreo(member.getRecommendId())){
            return "/newcoo/weixin/doLogin";
        }
        model.addAttribute("orderStatus", orderStatus);

        return "/newcoo/weixin/order/index";
    }
    /**
     * 创建订单
     * @param request
     * @param response
     * @param addressId
     * @param remark
     */
    @RequestMapping("/createOrder")
    public void createOrder(HttpServletRequest request,
                            HttpServletResponse response,
                            @RequestParam(value = "productId", required = true) Long productId,
                            @RequestParam(value = "addressId", required = true) Long addressId,
                            @RequestParam(value = "remark", required = false) String remark
    ){
        try{
            SundayWeixinMemberDto member= HttpRequestUtil.getUser(request);
            Map<String,Object> result=orderService.createOrder(member.getId(),productId,addressId,remark);

            ResponseMessage.success(result);
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code= CommonConstants.TOKEN_CODE;
                msg= CommonConstants.TOKENMSG;
            }else{
                logger.error("/coo/weixin/order/createOrder----e="+e.getMessage());
                e.printStackTrace();
            }
            ResponseMessage.error(code,msg);

        }
    }

    /**
     *
     * @param request
     * @param response
     * @param orderStatus
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="orderStatus",required=false)Integer orderStatus
    ) {
        try {
            SundayWeixinMemberDto member= HttpRequestUtil.getUser(request);
            List<CooOrderDto> orders=orderService.selectOrder(request, member.getId(), orderStatus);
            AjaxUtils.renderJson(response, orders);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**
     * 取消订单
     * @param request
     * @param response
     * @param orderId
     */
    @RequestMapping("/cancelOrder")
    public void cancleOrder(HttpServletRequest request,
                            HttpServletResponse response,
                            @RequestParam(value = "orderId", required = true) Long orderId
    ){
        try{
            orderService.cancelOrder(orderId);
            ResponseMessage.success();
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code= CommonConstants.TOKEN_CODE;
                msg= CommonConstants.TOKENMSG;
            }else{
                logger.error("/coo/weixin/order/cancleOrder----e="+e.getMessage());
                e.printStackTrace();
            }
            ResponseMessage.error(code,msg);

        }
    }

    /**
     * 评价订单
     * @param request
     * @param response
     * @param orderId
     */
    @RequestMapping("/commentOrder")
    public void commentOrder(HttpServletRequest request,
                             HttpServletResponse response,
                            @RequestParam(value = "orderId", required = true)Long orderId,
                            @RequestParam(value = "productIds", required = true) List<Long> productIds,
                             @RequestParam(value = "contents", required = true) List<String> contents,
                            @RequestParam(value = "scores", required = true)List<Integer> scores,
                            @RequestParam(value = "images", required = true)List<String> images
    ){
        try{
            SundayWeixinMemberDto member= HttpRequestUtil.getUser(request);
            orderService.commentOrder(orderId,productIds,contents,scores,images,member.getId());
            ResponseMessage.success();
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code= CommonConstants.TOKEN_CODE;
                msg= CommonConstants.TOKENMSG;
            }else{
                logger.error("/coo/weixin/order/commentOrder----e="+e.getMessage());
                e.printStackTrace();
            }
            ResponseMessage.error(code,msg);

        }
    }
    /**
     * 确认收货
     * @param request
     * @param response
     * @param orderId
     */
    @RequestMapping("/confirmReceive")
    public void confirmReceive(HttpServletRequest request,
                             HttpServletResponse response,
                             @RequestParam(value = "orderId", required = true)Long orderId
    ){
        try{
            SundayWeixinMemberDto member= HttpRequestUtil.getUser(request);
            orderService.confirmReceive(orderId,member.getId());
            ResponseMessage.success();
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code= CommonConstants.TOKEN_CODE;
                msg= CommonConstants.TOKENMSG;
            }else{
                logger.error("/coo/weixin/order/confirmReceive----e="+e.getMessage());
                e.printStackTrace();
            }
            ResponseMessage.error(code,msg);

        }
    }

}
